package com.onpositive.picture.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.net.UnknownHostException;

public class PictureGrabber {

	public byte[] grabWikiPictur( String path, int xRes, int yRes )
	{		
		if( !path.contains("upload.wikimedia.org") )
			return loadPicture(path) ;
		
		String newPath = buildThumbPath(path, xRes);
		if( newPath == null )
			return loadPicture(path) ;
		
		byte[] resultingArray = loadPicture(newPath);
		if( resultingArray == null )
			resultingArray = loadPicture(path);
		
		int xResNew = recalculateSize( resultingArray, xRes, yRes ) ;
		
		if( xResNew >= 0 ){
			newPath = buildThumbPath( path, xResNew ) ;
			if( newPath == null )
				resultingArray = loadPicture(path) ;
			else{			
				resultingArray = loadPicture(newPath) ;
				if( resultingArray == null )
					resultingArray = loadPicture(path);
			}
		}
		
		return resultingArray ;				
	}

	private int recalculateSize( byte[] resultingArray, int xRes, int yResMax ) {
		
		int yRes = getVerticalSize( resultingArray ) ;
		if( yRes <= yResMax )
			return -1 ;
		
		double ratio = ((double)yResMax)/yRes ;
		int xResNew = (int)( ratio * xRes + 0.5 );
		double newRatio = ((double)xResNew)/xRes ;
		int yResExpected = (int)(yRes * newRatio+0.5) ;
		return yResExpected > yResMax ? xResNew -1 : xResNew  ;
	}

	public String buildThumbPath(String path, int xRes ) {
		
		//http://upload.wikimedia.org/wikipedia/commons/c/cd/Su-27_low_pass.jpg
		//http://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/Su-27_low_pass.jpg/800px-Su-27_low_pass.jpg
		
		int ind0 = path.indexOf("commons/") ;
		
		if (ind0<0){
			String string = "http://upload.wikimedia.org/wikipedia/en/";
			if (path.startsWith(string)){
				int ind1 = path.lastIndexOf("/")+1 ;
				String picName = path.substring(ind1) ;
				ind0=string.length();
				StringBuilder bld = new StringBuilder() ;
				bld.append( path.substring(0, ind0 ) ) ;
				bld.append("thumb/") ;
				bld.append( path.substring(ind0 )) ;
				bld.append( "/" ) ;
				bld.append(xRes) ;
				bld.append("px-") ;
				bld.append(picName) ;
				
				String newPath = bld.toString() ;
				return newPath;
			}
			return null ;
		}
			
		
		int ind1 = path.lastIndexOf("/")+1 ;
		String picName = path.substring(ind1) ;
		
		StringBuilder bld = new StringBuilder() ;
		ind0 += "commons/".length() ;
		bld.append( path.substring(0, ind0 ) ) ;
		bld.append("thumb/") ;
		bld.append( path.substring(ind0 )) ;
		bld.append( "/" ) ;
		
		bld.append(xRes) ;
		bld.append("px-") ;
		bld.append(picName) ;
		
		String newPath = bld.toString() ;
		return newPath;
	}
	
	protected int getVerticalSize( byte[] arr ){
		
		return -1 ;
	}
	
	private byte[] loadPicture( String link  )
	{
		if( link == null )
			return null ;
		
		try {
							
			URL url = new URL( link );

			BufferedInputStream picStream = new BufferedInputStream( url.openStream() ) ;
				
			ByteArrayOutputStream os = new ByteArrayOutputStream() ;
				
			for( int n = picStream.read() ; n != -1 ; n = picStream.read() )
				os.write((byte)n);
			
			os.close();
				
			return os.toByteArray() ;
		} catch (IOException e) {
			if (e instanceof UnknownHostException){
				throw new NoConnectionException();
			}
			if (e instanceof ConnectException){
				throw new NoConnectionException();
			}
			System.out.print( "cannot find picture: " ) ;
			System.out.print( link ) ;
			System.out.print( "\n" ) ;
		}
		return null ;
	}
}
